package com.pengtu.utils.jbpm.rollback;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jbpm.api.identity.Group;
import org.jbpm.api.identity.User;
import org.jbpm.pvm.internal.identity.spi.IdentitySession;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.spring.SpringContextHolder;

@Component
@Transactional
public class UserIdentitySessionImpl implements IdentitySession {

//	private AccountManager accountManager = SpringContextHolder.getBean("accountManager");

	@Override
	public String createUser(String userId, String givenName,
			String familyName, String businessEmail) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User findUserById(String userId) {
		// TODO Auto-generated method stub
		AccountManager accountManager = SpringContextHolder.getBean("accountManager");
		return accountManager.getUser(userId);
	}

	@Override
	public List<User> findUsersById(String... userIds) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findUsers() {
		// TODO Auto-generated method stub
		AccountManager accountManager = SpringContextHolder.getBean("accountManager");
		List<com.pengtu.entity.app.User> users = accountManager.get();
		List<User> list=new ArrayList<User>();
		for(User user:users){
			list.add((User)user);
		}
		return list;
	}

	@Override
	public void deleteUser(String userId) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public String createGroup(String groupName, String groupType,
			String parentGroupId) {
		// TODO Auto-generated method stub
		return null;
	}

	//---------------------------------methods above are not need overwrite--------------------------------------------
	
	@Override
	public List<User> findUsersByGroup(String groupId) {
		AccountManager accountManager = SpringContextHolder.getBean("accountManager");
		List<com.pengtu.entity.app.User> userList = accountManager.findUsersByOrgId(groupId);
		List<User> list=new ArrayList<User>();
		for(User user : userList){
			list.add(user);
		}
		return list;
	}

	@Override
	public Group findGroupById(String groupId) {
		// TODO Auto-generated method stub
		AccountManager accountManager = SpringContextHolder.getBean("accountManager");
		return accountManager.getRole(groupId);
	}

	@Override
	public List<Group> findGroupsByUserAndGroupType(String userId,
			String groupType) {
		// TODO Auto-generated method stub
		return findGroupsByUser(userId);
	}

	@Override
	public List<Group> findGroupsByUser(String userId) {
		// TODO Auto-generated method stub
		AccountManager accountManager = SpringContextHolder.getBean("accountManager");
		com.pengtu.entity.app.User user = accountManager.getUser(userId);
		List<Group> list=new ArrayList<Group>();
		Iterator<?> it=user.getRoleList().iterator();
		while(it.hasNext()){
			list.add((Group)it.next());
		}
		return list;
	}

	@Override
	public void deleteGroup(String groupId) {
		// TODO Auto-generated method stub

	}

	@Override
	public void createMembership(String userId, String groupId, String role) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteMembership(String userId, String groupId, String role) {
		// TODO Auto-generated method stub

	}

}
